import { Controller, Get, Post, Body, Patch, Param, Delete, ParseIntPipe, Req, Query, SetMetadata, UseGuards, Inject, Injectable } from '@nestjs/common';
import { HomeService } from './home.service';
import { CreateHomeDto } from './dto/create-home.dto';
import { UpdateHomeDto } from './dto/update-home.dto';
import { HomePipe } from './home.pipe'; 
import { HomeDecorator } from './home.decorator';
import { HomeGuard } from './home.guard';
import { WINSTON_LOGGER_TOKEN } from 'src/log/logger.module';

@Controller('home')
@Injectable()
export class HomeController {
  // 注入日志
  @Inject(WINSTON_LOGGER_TOKEN)
  private logger;

  constructor(private readonly homeService: HomeService) {}

  // 自定义装饰器结合路由首位在进行权限校验
  @Get('guard')
  @HomeDecorator(['admin', 'guest', 'master'])
  @UseGuards(HomeGuard)
  guardValite(@Query('role') role: string) {
    return {
      code: 200,
      data: role
    }
  }
  

  // ParseIntPipe 管道转换，将前端string 类型转成 int
  @Get(':id')
  findOne(@Param('id', ParseIntPipe) id: string) {
    this.logger.log('类型', typeof id);
    return {
      data: `拿到的数据类型是 ${typeof id}`,
      code: 200
    };
  }

  // 获取生成的验证id
  @Post('create')
  create(@Body() body) {
    const {name , age} = body;
    const id: string = this.homeService.createId(name, age);
    return {
      code: 200,
      data: id
    }
  }

  // HomePipe 管道验证接口
  @Post('pipe')
  pipeValite(@Body(HomePipe) createHomeDto: CreateHomeDto) {
    const id: string = this.homeService.getPipeValite(createHomeDto);
    return {
      code: 200,
      data: id
    }
  }

}
